﻿<%--
  Created by IntelliJ IDEA.
  User: bb
  Date: 2021/10/16
  Time: 9:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <%--不缓存--%>
    <META HTTP-EQUIV="refresh" CONTENT="1">

    <title>Title</title>
    <link href="jquery-easyui-1.7.0/themes/default/easyui.css" rel="stylesheet" type="text/css" />
    <link href="jquery-easyui-1.7.0/themes/icon.css" rel="stylesheet" type="text/css" />
    <link href="jquery-easyui-1.7.0/demo/demo.css" rel="stylesheet" type="text/css" />
    <script src="jquery-easyui-1.7.0/jquery.min.js" type="text/javascript"></script>
    <script src="jquery-easyui-1.7.0/jquery.easyui.min.js" type="text/javascript"></script>
    <script src="jquery-easyui-1.7.0/locale/easyui-lang-zh_CN.js" type="text/javascript"></script>
</head>
<body>
    <table id="datagrid_product" class="easyui-datagrid"></table>
    <%--查询工具栏--%>
    <div id="searchBar" style="padding: 2px 5px;">
        <form id="searchForm_product" method="post">
            <div style="padding: 3px">
                商品编号&nbsp;&nbsp;<input class="easyui-textbox" id="p_code" name="p_code" style="width: 110px"/>
            </div>
            <div style="padding: 3px">
                商品名称&nbsp;&nbsp;<input class="easyui-textbox" id="p_name" name="p_name" style="width: 110px"/>&nbsp;&nbsp;
                商品类型&nbsp;&nbsp;<input class="easyui-combobox" id="p_tid" name="p_tid"  style="width: 110px"
                                       data-options="valueField:'id',textField:'name',url:'type/getType/1'" value="0"/>&nbsp;&nbsp;
                商品品牌&nbsp;&nbsp;<input class="easyui-textbox" id="p_brand" name="p_brand" style="width: 110px"/>&nbsp;&nbsp;
                价格：<input class="easyui-numberbox" id="p_priceFrom" name="p_priceFrom" style="width: 80px;" value="0"/>
                ~ <input class="easyui-numberbox" id="p_priceTo" name="p_priceTo" style="width: 80px;"/>&nbsp;&nbsp;
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-search"  onclick="searchProduct();" plain="true">查找</a>
            </div>
        </form>
    </div>
    <%--操作工具栏--%>
    <div id="table_product" style="padding: 2px 5px;">
      <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="addProduct();">添加</a>
      <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editProduct();">修改</a>
      <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="off_loadingProduct();">下架</a>
      <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" plain="true" onclick="removeProduct();">删除</a>
    </div>
    <%--商品dialog--%>
    <div id="dialog_productAdd" class="easyui-dialog" title="添加商品" closed="true" style="width: 500px">
        <div style="padding: 10px 60px 20px 60px">
            <form id="Upload_product" method="post" action="" enctype="multipart/form-data">
                <table cellpadding="5">
                    <tr>
                        <td>商品状态：</td>
                        <td><select id="status" class="easyui-combobox" name="status" style="width: 150px;">
                            <option value="1">在售</option>
                            <option value="0">下架</option>
                        </select></td>
                    <tr>
                    <tr>
                        <td>商品类型</td>
                        <td>
                            <input id="type.id" class="easyui-combobox" name="type.id"
                                   data-options="valueField:'id',textField:'name',url:'type/getType/0'"/>
                        </td>
                    </tr>
                    <tr>
                        <td>商品名称</td>
                        <td><input class="easyui-textbox" type="text" id="name" name="name" data-options="required:true"/></td>
                    </tr>
                    <tr>
                        <td>商品编码</td>
                        <td><input class="easyui-textbox" type="text" id="code" name="code" data-options="required:true"/></td>
                    </tr>
                    <tr>
                        <td>商品品牌</td>
                        <td><input class="easyui-textbox" type="text" id="brand" name="brand" data-options="required:true"/></td>
                    </tr>
                    <tr>
                        <td>商品数量</td>
                        <td><input class="easyui-textbox" type="text" id="num" name="num" data-options="required:true"/></td>
                    </tr>
                    <tr>
                        <td>商品价格</td>
                        <td><input class="easyui-textbox" type="text" id="price" name="price" data-options="required:true"/></td>
                    </tr>
                    <tr>
                        <td>商品描述</td>
                        <td><input class="easyui-textbox" type="text" id="intro" name="intro" data-options="multiline:true"/></td>
                    </tr>
                    <tr>
                        <td>商品图片</td>
                        <td><input class="easyui-filebox" id="file" name="file" style="width: 200px" value="选择图片"/></td>
                    </tr>
                </table>
            </form>
            <div style="text-align: center; padding: 5px;">
                <a class="easyui-linkbutton" href="javascript:void(0)" onclick="saveProduct();">保存</a>
                <a class="easyui-linkbutton" href="javascript:void(0)" onclick="clearForm();">清空</a>
            </div>
        </div>
    </div>
    <%------------------------------------------------script---------------------------------------------------------------%>
    <script>
        $(function (){
            $('#datagrid_product').datagrid({
                singleSelect:true,//设置为多选
                pagination:true,//启用分页
                pageSizes:10,//设置每页记录数
                pageList:[5,10,15,20],//设置可供选择的页大小
                rownumbers:true,//显示行号
                fit:true,//自适应
                fitColumns:true,
                header:'#searchBar',//为datagrid添加搜索栏
                toolbar:'#table_product',//为datagrid添加工具栏
                url:'product/list',//为datagrid设置数据源
                columns:[[{ //编辑datagrid的列
                    title:'序号',
                    field:'id',
                    align:'center',
                    checkbox:true
                },{
                    title:'商品名称',
                    field:'name',
                },{
                    title:'商品类型',
                    field:'type',
                    formatter:function (value,row,index){
                        if (row.type)
                            return row.type.name;
                        return value;
                    }
                },{
                    title:'商品状态',
                    field:'status',
                    formatter:function (value,row,index){
                        if (row.status == 1)
                            return "在售";
                        return "下架";
                    }
                },{
                    title:'商品编码',
                    field:'code',
                },{
                    title:'品牌',
                    field:'brand',
                },{
                    title:'商品价格',
                    field:'price',
                },{
                    title:'商品描述',
                    field:'intro',
                    width:150
                },{
                    title:'商品数量',
                    field:'num',
                },{
                    title: '商品图片',
                    field:'pic'
                }]]
            });
        });
    </script><%--datagrid--%>
    <script>
        function searchProduct(){
            var  p_code = $('#p_code').textbox("getValue");
            var  p_name = $('#p_name').textbox("getValue");
            var  p_tid = $('#p_tid').textbox("getValue");
            var  p_brand = $('#p_brand').textbox("getValue");
            var  priceFrom;
            var  priceTo;
            if ( $("#p_priceFrom").val() != null && $("#p_priceFrom").val() != "" ){
                priceFrom = $("#p_priceFrom").textbox("getValue");
            }else {
                priceFrom = 0;
            }
            if ( $("#p_priceTo").val() != null && $("#p_priceTo").val() != "" ){
                priceTo = $("#p_priceTo").textbox("getValue");
            }else {
                priceTo = 0;
            }
            //console.log(priceFrom,priceTo);//调试
            $("#datagrid_product").datagrid('load',{
                "code":p_code,
                "name":p_name,
                "type.id":p_tid,
                "brand":p_brand,
                "priceFrom":priceFrom,
                "priceTo":priceTo,
            });
        }
    </script><%--查找商品--%>
    <script>
        function clearForm() { //清空
            $("#dialog_productAdd").form("clear");
        }
        function addProduct(){
            $('#dialog_productAdd').dialog('open').dialog('setTitle','新增商品');
            clearForm();
            addProductUrl = 'product/addProduct'; //全局变量
        }
        function saveProduct(){
            $('#Upload_product').form("submit",{
                url:addProductUrl,
                success:function (result){
                    var result1 = eval('('+result+')');
                    if ( result1.success == 'true' ){
                        $('#datagrid_product').datagrid("reload");
                        $('#dialog_productAdd').dialog("close");
                        //console.log("true");//调试
                    }
                    //console.log(result1.message);//调试
                    $.messager.show({
                        title:"提示信息",
                        msg:result1.message
                    });
                }
            })
        }
    </script><%--添加商品--%>
    <script>
        function off_loadingProduct(){
            var rows = $('#datagrid_product').datagrid("getSelections");
            if(rows.length > 0){
                //$('#').dialog('open').dialog('setTitle','删除商品');
                $.messager.confirm('Confirm', '确定要下架选中的商品吗？', function (r){
                   if (r) {
                       var ids = "";
                       var len = rows.length;
                       for (var i = 0 ; i <  len; i++){
                           ids += rows[i].id ;
                           if (!i+1 == len)
                               ids += ','
                       }
                       $.post('product/off-loading', {
                           id : ids,
                           flag : 0//下架标记
                       },function (result){
                           if (result.success == "true"){
                               $('#datagrid_product').datagrid("reload");
                               $.messager.show({
                                   title:"提示信息",
                                   msg:result.message
                               });
                           }else {
                               $.messager.show({
                                   title:"提示信息",
                                   msg:result.message
                               });
                           }
                       },'json');
                   }
                });
            }else
                $.messager.alert('提示', '请选择要下架的商品', 'info');
        }
    </script><%--下架商品--%>
    <script>
        function editProduct(){
            var rows = $('#datagrid_product').datagrid("getSelections");
            if ( rows.length == 1 ){
                var row = $('#datagrid_product').datagrid("getSelected");
                if (row){
                    $('#dialog_productAdd').dialog('open').dialog('setTitle','修改商品信息');//与新增商品公用一个dialog
                    $('#Upload_product').form("load",{
                        "name":row.name,
                        "code":row.code,
                        "status":row.status,
                        "brand":row.brand,
                        "num":row.num,
                        "price":row.price,
                        "intro":row.intro,
                        "type.id":row.type.id
                    });
                    addProductUrl = "product/updateProduct?id=" + row.id;
                    //$.post(url,saveProduct())
                }
            }else if( rows.length < 1 ){
                $.messager.alert('提示','请选择一个要修改的商品','info');
            }else{
                $.messager.alert('提示','一次请选择一个','info');
            }
        }
    </script><%--编辑商品--%>
    <script>
        function removeProduct(){
            var rows = $('#datagrid_product').datagrid("getSelections");
            if(rows.length > 0){
                $.messager.confirm('Confirm', '确定要删除选中的商品吗？', function (r){
                    if (r) {
                        var ids = "";
                        var len = rows.length;
                        for (var i = 0 ; i <  len; i++){
                            ids += rows[i].id ;
                            if (!i+1 == len)
                                ids += ','
                        }
                        $.post('product/deleteProduct', {
                            id : ids
                        },function (result){
                            if (result.success == "true"){
                                $('#datagrid_product').datagrid("reload");
                                $.messager.show({
                                    title:"提示信息",
                                    msg:result.count + "条商品信息" + result.message
                                });
                            }else {
                                $.messager.show({
                                    title:"提示信息",
                                    msg:result.message
                                });
                            }
                        },'json');
                    }
                });
            }else
                $.messager.alert('提示', '请选择要删除的商品', 'info');
        }
    </script><%--删除商品--%>
</body>
</html>
